Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created June 1, 2024 20:56
Shared via mypy Playground
import typing as t
class D:
def __set__(self, obj: t.Any, val: int, /) -> None: ...
class A:
desc = D()
desc_final: t.Final = D()
a: A = A()
@mypy-play
mypy-play / main.py
Created June 1, 2024 20:50
Shared via mypy Playground
import typing as t
class A:
@property
@t.final
def prop(self) -> int:
return 0
@prop.setter
@t.final
@mypy-play
mypy-play / main.py
Created June 1, 2024 20:49
Shared via mypy Playground
import typing as t
class A:
@property
@t.final
def prop(self) -> int:
return 0
@prop.setter
@t.final
@mypy-play
mypy-play / main.py
Created June 1, 2024 20:46
Shared via mypy Playground
import typing as t
class D:
def __set__(self, obj: t.Any, val: int, /) -> None: ...
class A:
desc: t.Final[D] = D()
@property
@t.final
@mypy-play
mypy-play / main.py
Created May 31, 2024 21:46
Shared via mypy Playground
from dataclasses import dataclass
@dataclass
class A:
foo: int
@dataclass
class B:
bar: int
@mypy-play
mypy-play / main.py
Created May 31, 2024 20:13
Shared via mypy Playground
class Bar:
pass
def foo() -> "Bar | None":
return None
@mypy-play
mypy-play / main.py
Created May 31, 2024 17:29
Shared via mypy Playground
from typing import Union
def foo(x: Union[int, float, str]): print(str(x))
x: Union[int, Union[float, str]] = 7
y: Union[int, Union[float, str]] = 3.0
z: Union[int, Union[float, str]] = "hi"
foo(x)
foo(y)
foo(z)
@mypy-play
mypy-play / main.py
Created May 31, 2024 17:02
Shared via mypy Playground
from typing import assert_type, Any, Optional, TypeVar, overload
T = TypeVar("T")
@overload
def get(key: str) -> Optional[Any]: ...
@overload
def get(key: str, default: Any | T) -> Any | T: ...
@mypy-play
mypy-play / main.py
Created May 31, 2024 15:37
Shared via mypy Playground
from collections.abc import Iterable
from enum import StrEnum
def takes_enum(e: StrEnum) -> StrEnum:
return e
def takes_enum_type(type_: type[StrEnum]) -> StrEnum:
return next(iter(type_)) # error: Incompatible return value type (got "str", expected "StrEnum") [return-value]
def takes_enum_type_with_trick(type_: type[StrEnum]) -> StrEnum:
@mypy-play
mypy-play / main.py
Created May 31, 2024 15:35
Shared via mypy Playground
from collections.abc import Iterable
from enum import StrEnum
def takes_enum(e: StrEnum) -> StrEnum:
return e
def takes_enum_type(type_: type[StrEnum]) -> StrEnum:
return next(iter(type_))
def takes_enum_type_with_trick(type_: type[StrEnum]) -> StrEnum: